package com.example.controller;

import com.example.entity.Role;
import com.example.service.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @GetMapping("/detail")
    public String getRoleDetail(@RequestParam(required = false) Long roleId,
                                @RequestParam(required = false) String roleKey,
                                Model model) {
        Role role = null;
        if (roleId != null) {
            role = roleService.getRoleWithMenusById(roleId);
        } else if (roleKey != null) {
            role = roleService.getRoleWithMenusByRoleKey(roleKey);
        }
        model.addAttribute("role", role);
        return "role/detail";
    }

    @GetMapping("/list")
    public String roleList(Model model) {
        List<Role> roles = roleService.getAllRoles();
        model.addAttribute("roles", roles);
        return "role/list";
    }
}
